package cn.huihuo.weibo.module;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by Administrator on 2015/8/26.
 */
public class Comment extends Model<Comment> {

    public static final Comment comment = new Comment();

    public Page<Comment> findByProdCommentId(int pageNumber, int pageSize, String prodCommtId) {
        return super.paginate(pageNumber, pageSize, "SELECT comm.id commId,comm.ugc_id,comm.comment COMMENT,u.id userId,u.nickname nickname,u.avatar avatar,comm.comment_id commCommId,comm.comment_time commentTime ", " FROM COMMENT comm, USER u WHERE comm.user_id = u.id and comm.ugc_id= ? order by comment_time desc  ", prodCommtId);
    }

    public List<Comment> findByProdComment(String prodCommentId) {
        return find("select * from comment t where t.prod_comment_id=?", prodCommentId);
    }

    public Comment findCommentSizeByUGC(String ugcId){
        return findFirst("select count(1) size from comment where ugc_id = ?",ugcId);
    }

    public Comment findByCommId(String commId) {
        return findFirst("SELECT comm.id commId,comm.comment COMMENT,u.id userId,u.nickname nickname,comm.comment_id commCommId,comm.comment_time commentTime FROM COMMENT comm, USER u WHERE comm.user_id = u.id and comm.id= ?", commId);
    }

    public Comment findProdCommByCommId(Integer commId){
        return findFirst("select pc.id ugcId ,pc.product_comment myComment ,pc.ugc_type ugcType,c.user_id userId,u.nickname,u.avatar,c.comment replayComment ,c.id replayId from prodcomment pc,comment c,user u where u.id=c.user_id and pc.id=c.ugc_id and c.id=?",commId);
    }

    public boolean deleteByProdCommId(String prodCommenId){
        return Db.update("delete from COMMENT where ugc_id = ?", prodCommenId)==0?false:true;
    }

    public List<Comment> findCommentByUGCId(String ugcId){
        return find("select * from COMMENT where ugc_id = ?", ugcId);
    }

    public Comment findReplayCommByCommId(Integer commId){
        return findFirst("SELECT repcom.id replayId,repcom.ugc_id ugcId, repcom.comment replayComment,mycom.comment myComment,u.id userId,u.nickname,u.avatar FROM COMMENT mycom, COMMENT repcom, USER u WHERE u.id=mycom.user_id AND mycom.comment_id = repcom.id AND mycom.id=?",commId);
    }

    public Comment getCommentSize(Integer ugcId){
        return findFirst("select count(*) size from comment where ugc_id=?",ugcId);
    }

    public Comment getCommUserSize(Integer ugcId){
        return findFirst("select count(1) size from( select distinct t.user_id from comment t where t.ugc_id=? ) s",ugcId);
    }

    public List<Comment> findByReplayCommId(Integer replayId){
        return find("select * from comment t where t.comment_id=?",replayId);
    }
}
